/**
 * 
 */
package com.rst.user.domain;

import java.util.Collection;
import java.util.HashSet;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.rst.common.domain.CommonDetailsTable;


/**
 * @author JavaDeamon
 *
 */
@Entity
@Table(name="USER_DETAILS")
public class UserDetailsDTO extends CommonDetailsTable implements UserDetails {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7854551745658888222L;
	private String username;
	private String userDisplayName;
	private String emailId;
	private String phoneNumber;
	private String password;
	
	private boolean isAccountNonExpired;
	private boolean isAccountNonLocked;
	private boolean isCredentialsNonExpired;
	private boolean isEnabled;
	
	
	private Collection<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
	
	
	/**
	 * @return the userId
	 * is primary key
	 * column contains login id for the user
	 * column cannot be null
	 * maximum length is 30
	 */
	@Id
	@Column(name="USER_ID", nullable=false, length=30)
	public String getUsername() {
		return username;
	}
	/**
	 * @return the userName
	 * column contains name of the user
	 * column cannot be null
	 * maximum length is 50
	 */
	@Column(name="USER_NAME", nullable=false, length=50)
	public String getUserDisplayName() {
		return userDisplayName;
	}
	/**
	 * @return the emailId
	 * contains email of the user in a specified format
	 * column cannot be null
	 * maximum length is 60
	 */
	@Column(name="USER_EMAILID", nullable=false, length=60)
	public String getEmailId() {
		return emailId;
	}
	/**
	 * @return the phoneNumber
	 * contains phone number of the user.
	 * can be mobile or landline number
	 * column cannot be null
	 * maximum length is 20
	 */
	@Column(name="USER_PHONE", nullable=false, length=20)
	public String getPhoneNumber() {
		return phoneNumber;
	}
	/**
	 * @return the password
	 * contains password for the user
	 * can be in encrypted format
	 * column cannot be null
	 * maximum length is 200
	 */
	@Column(name="USER_PASSWORD", nullable=false, length=200)
	public String getPassword() {
		return password;
	}
	/**
	 * @return the isAccountNonExpired
	 * boolean field of spring security to maintain the expiry of account
	 * cannot be null
	 * default value 1
	 */
	@Column(name="IS_ACCOUNT_EXPIRED", nullable=false, columnDefinition="smallint default 1")
	public boolean isAccountNonExpired() {
		return isAccountNonExpired;
	}
	/**
	 * @return the isAccountNonLocked
	 * boolean field to maintain locking of account
	 * cannot be null
	 * default value 1
	 */
	@Column(name="IS_ACCOUNT_LOCKED", nullable=false, columnDefinition="smallint default 1")
	public boolean isAccountNonLocked() {
		return isAccountNonLocked;
	}
	/**
	 * @return the isCredentialsNonExpired
	 * boolean field to maintain expiry of credentials
	 * cannot be null
	 * default value 1
	 */
	@Column(name="IS_CREDENTIALS_EXPIRED", nullable=false, columnDefinition="smallint default 1")
	public boolean isCredentialsNonExpired() {
		return isCredentialsNonExpired;
	}
	/**
	 * @return the isEnabled
	 * Boolean field to maintain account is enabled or not
	 * cannot be null
	 * default value 1
	 */
	@Column(name="IS_ENABLED", nullable=false, columnDefinition="smallint default 1")
	public boolean isEnabled() {
		return isEnabled;
	}
	/**
	 * contains authorities of user
	 * has manytoone mapping with role details
	 * makes table user role mapping by join table
	 * fetch type is eager
	 */
	@ManyToOne(targetEntity=RoleDetailsDTO.class, fetch=FetchType.EAGER)
	@JoinTable(name="USER_ROLE_MAPPING", joinColumns=@JoinColumn(name="USER_ID"), inverseJoinColumns=@JoinColumn(name="ROLE_ID"))
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}
	
	/**
	 * @param userId the userId to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}
	/**
	 * @param userName the userName to set
	 */
	public void setUserDisplayName(String userDisplayName) {
		this.userDisplayName = userDisplayName;
	}
	/**
	 * @param emailId the emailId to set
	 */
	public void setEmailId(String emailId) {
		this.emailId = emailId;
	}
	/**
	 * @param phoneNumber the phoneNumber to set
	 */
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 * @param isAccountNonExpired the isAccountNonExpired to set
	 */
	public void setAccountNonExpired(boolean isAccountNonExpired) {
		this.isAccountNonExpired = isAccountNonExpired;
	}
	/**
	 * @param isAccountNonLocked the isAccountNonLocked to set
	 */
	public void setAccountNonLocked(boolean isAccountNonLocked) {
		this.isAccountNonLocked = isAccountNonLocked;
	}
	/**
	 * @param isCredentialsNonExpired the isCredentialsNonExpired to set
	 */
	public void setCredentialsNonExpired(boolean isCredentialsNonExpired) {
		this.isCredentialsNonExpired = isCredentialsNonExpired;
	}
	/**
	 * @param isEnabled the isEnabled to set
	 */
	public void setEnabled(boolean isEnabled) {
		this.isEnabled = isEnabled;
	}
	/**
	 * @param authorities the authorities to set
	 */
	public void setAuthorities(Collection<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "UserDetails [userId=" + username + ", userName=" + userDisplayName
				+ ", emailId=" + emailId + ", phoneNumber=" + phoneNumber + "]";
	}
}
